﻿#include "msaddcondialog.h"
#include "ui_msaddcondialog.h"

#include <QSqlQuery>

int MSaddconDialog::sendid;
int MSaddconDialog::remain;
MSaddconDialog::MSaddconDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MSaddconDialog)
{
    ui->setupUi(this);
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("添加信息状态：");
    connect(ui->cancelbtn,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(this,SIGNAL(sendID(int)),parent,SLOT(MSshowcontent(int)));
}

MSaddconDialog::~MSaddconDialog()
{delete ui;}

void MSaddconDialog::receivedata(int sID,int num)
{
     sendid=sID;
     remain=num;
}

void MSaddconDialog::on_ackbtn_clicked()
{
    if(ui->nameedit->text().isEmpty()||ui->materialedit->text().isEmpty())
        {ui->status->setText("添加信息状态:请添入完整信息");}
    else{
    QSqlQuery query;
    query.prepare("INSERT INTO ZZB.Materials(MaterialsID,Name,MaterialName,SendID)"
                  "VALUES(:MaterialsID,:Name,:MaterialName,:SendID)");
    query.bindValue(":Name",ui->nameedit->text());
    query.bindValue(":MaterialName",ui->materialedit->text());
    query.bindValue(":SendID",sendid);

    bool ok=query.exec();
    if(ok)
    {ui->status->setText("添加信息状态:添加成功");
     emit sendID(sendid);
     ui->nameedit->clear();
     ui->materialedit->clear();
    remain--;
    if(remain==0){this->close();}
    }else{ui->status->setText("添加信息状态:添加失败");}
}}
